package com.univrz.flexmachine.scmxml.v
{
	
	import com.univrz.flexmachine.scxml.model.Node;
	import com.univrz.flexmachine.scxml.model.SCXMLCoreInterpreter;
	import com.univrz.flexmachine.scxml.model.SCXMLModelObject;
	import com.univrz.flexmachine.scxml.model.StateNode;
	
	import flash.events.Event;
	
	import mx.containers.Panel;
	import mx.core.UIComponentGlobals;


//--------------------------------------
//  Excluded APIs
//--------------------------------------

// we sync title and label to ID
// we should exclude everything...
// perhaps the way to go is to implement a new UIComponent altogether...
[Exclude(name="label", kind="property")]


/**
 *  
 * 
 *  @eventType com.univrz.flexmachine.events.StateMachineEvent.ENTER
 */
[Event(name="enter", type="com.univrz.flexmachine.events.StateMachineEvent")]		



/**
 *  
 * 
 *  @eventType com.univrz.flexmachine.events.StateMachineEvent.EXIT
 */
[Event(name="exit", type="com.univrz.flexmachine.events.StateMachineEvent")]	



	public class State extends Panel
	{
		
		
		
		private var node:Node; 
		
		
		
		///////////////////////////////////////////
		// isActive
		///////////////////////////////////////////
		
		[Bindable("isActiveChanged")]
		public function get isActive( ):Boolean
		{
			return node.isActive;
		}
		
		
		
		
		public function State( )
		{
			super( );
			
			setStyle( "borderThicknessBottom"	, 3 );
			setStyle( "borderThicknessTop"		, 3 );
			setStyle( "borderThicknessLeft"		, 3 );
			setStyle( "borderThicknessRight"		, 3 );			

			setStyle( "headerHeight", 10 );
			
		}
		
		
		
		
		override protected function initializationComplete( ):void
		{
			
			super.initializationComplete( );
			
			
			if ( UIComponentGlobals.designMode )
			{
				// if we are in design mode we don't start the engine
			}
			else
			{
				
				if ( ! ( parent is State ) ) // make sure this is the outmost state
				{
					var rootStateNode:StateNode = SCMXMLVUtil.compile( this as State );
					// instantiate the interpreter
					var engine:SCXMLCoreInterpreter = new SCXMLCoreInterpreter( rootStateNode );
					engine.start( );
				}
				
			}
		
		}
		
		
		
		
		
		public function setModelObject( mobj:SCXMLModelObject ):void
		{
			// this will only be set once.
			node = mobj as Node;
			node.addEventListener( Event.CHANGE, node_change );
			update( );		
		}
	
	
		private function node_change( event:Event ):void
		{
			update( );			
		}
		

		
		private function update( ):void
		{
			dispatchEvent( new Event( "isActiveChanged" ) );
			setStyle( "backgroundColor", isActive ? 0xff0000 : 0xffffff );
		}
		
		
		
		
		override protected function commitProperties( ):void
		{
	
			super.commitProperties( );
			
            // titleTextField.text = id; ( not in design mode )
            
            // Don't call layoutChrome() if we  haven't initialized,
            // because it causes commit/measure/layout ordering problems
            // for children of the control bar.
            if (initialized)
                layoutChrome( unscaledWidth, unscaledHeight );
		
		}
		
	}
	
}
 